[Tutorial] Adding new Tracker Method for dummies 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*---------------STEP 1---------------------*/
  2. /* modify this file
  3. * opencv2/tracking/tracker.hpp
  4. * and put several lines of snippet similar to
  5. * the following:
  6. */
  7. /*------------------------------------------*/
  8. class CV_EXPORTS_W TrackerKCF : public Tracker
  9. {
  10. public:
  11. struct CV_EXPORTS Params
  12. {
  13. Params();
  14. void read( const FileNode& /*fn*/ );
  15. void write( FileStorage& /*fs*/ ) const;
  16. };
  17. /** @brief Constructor
  18. @param parameters KCF parameters TrackerKCF::Params
  19. */
  20. BOILERPLATE_CODE("KCF",TrackerKCF);
  21. };
  22. /*---------------STEP 2---------------------*/
  23. /* modify this file
  24. * src/tracker.cpp
  25. * add one line in function
  26. * Ptr<Tracker> Tracker::create( const String& trackerType )
  27. */
  28. /*------------------------------------------*/
  29. Ptr<Tracker> Tracker::create( const String& trackerType )
  30. {
  31. BOILERPLATE_CODE("MIL",TrackerMIL);
  32. BOILERPLATE_CODE("BOOSTING",TrackerBoosting);
  33. BOILERPLATE_CODE("MEDIANFLOW",TrackerMedianFlow);
  34. BOILERPLATE_CODE("TLD",TrackerTLD);
  35. BOILERPLATE_CODE("KCF",TrackerKCF); // add this line!
  36. return Ptr<Tracker>();
  37. }
  38. /*---------------STEP 3---------------------*/
  39. /* make a new file and paste the snippet below
  40. * and modify it according to your needs.
  41. * also make sure to put the LICENSE part.
  42. * src/trackerKCF.cpp
  43. */
  44. /*------------------------------------------*/
  45. /*---------------------------
  46. | TrackerKCFModel
  47. |---------------------------*/
  48. namespace cv{
  49. /**
  50. * \brief Implementation of TrackerModel for MIL algorithm
  51. */
  52. class TrackerKCFModel : public TrackerModel{
  53. public:
  54. TrackerKCFModel(TrackerKCF::Params /*params*/){}
  55. ~TrackerKCFModel(){}
  56. protected:
  57. void modelEstimationImpl( const std::vector<Mat>& responses ){}
  58. void modelUpdateImpl(){}
  59. };
  60. } /* namespace cv */
  61. /*---------------------------
  62. | TrackerKCF
  63. |---------------------------*/
  64. namespace cv{
  65. /*
  66. * Prototype
  67. */
  68. class TrackerKCFImpl : public TrackerKCF{
  69. public:
  70. TrackerKCFImpl( const TrackerKCF::Params &parameters = TrackerKCF::Params() );
  71. void read( const FileNode& fn );
  72. void write( FileStorage& fs ) const;
  73. protected:
  74. bool initImpl( const Mat& image, const Rect2d& boundingBox );
  75. bool updateImpl( const Mat& image, Rect2d& boundingBox );
  76. TrackerKCF::Params params;
  77. };
  78. /*
  79. * Constructor
  80. */
  81. Ptr<TrackerKCF> TrackerKCF::createTracker(const TrackerKCF::Params &parameters){
  82. return Ptr<TrackerKCFImpl>(new TrackerKCFImpl(parameters));
  83. }
  84. TrackerKCFImpl::TrackerKCFImpl( const TrackerKCF::Params &parameters ) :
  85. params( parameters )
  86. {
  87. isInit = false;
  88. }
  89. void TrackerKCFImpl::read( const cv::FileNode& fn ){
  90. params.read( fn );
  91. }
  92. void TrackerKCFImpl::write( cv::FileStorage& fs ) const{
  93. params.write( fs );
  94. }
  95. bool TrackerKCFImpl::initImpl( const Mat& image, const Rect2d& boundingBox ){
  96. model=Ptr<TrackerKCFModel>(new TrackerKCFModel(params));
  97. return true;
  98. }
  99. bool TrackerKCFImpl::updateImpl( const Mat& image, Rect2d& boundingBox ){return true;}
  100. /*
  101. * Parameters
  102. */
  103. TrackerKCF::Params::Params(){
  104. }
  105. void TrackerKCF::Params::read( const cv::FileNode& fn ){
  106. }
  107. void TrackerKCF::Params::write( cv::FileStorage& fs ) const{
  108. }
  109. } /* namespace cv */